/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ventanas;

import controlador.CtrlVista;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import negocio.Complejo;
import negocio.Fila;
import negocio.Sala;
import negocio.Seccion;
import utilidades.OrdListModel;

/**
 *
 * @author diego
 */
public class FrmFila extends javax.swing.JDialog {
    private CtrlVista vista;
    private Sala unaSala;
    private Seccion unaSeccion;    
    private Fila unaFila;
    
    OrdListModel modelSalas = new OrdListModel();    
    OrdListModel modelSecciones = new OrdListModel();
    OrdListModel modelFilas = new OrdListModel();
    
    

    /**
     * Creates new form FrmFila
     */
    public FrmFila(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        try{
            this.vista = new CtrlVista();
        }
        catch(Exception e){            
            JOptionPane.showMessageDialog(this, "Error de Inicializacion");
        }
        this.inicializar();
    }
        
    public void inicializar(){
        this.lstSalas.setModel(modelSalas);
        this.lstSecciones.setModel(modelSecciones);
        this.lstFilas.setModel(modelFilas);
        this.cargarSalas();
    }
    
    public void cargarSalas(){
        if(this.vista.obtenerSalas().size() > 0){
        for (Sala unaSala : this.vista.obtenerSalas()) {
            this.modelSalas.addElement(unaSala);
        }        
        }else{
            this.modelSalas.addElement("No hay salas disponibles");
        }        
    }
    
    public void cargarSecciones(){
         this.modelSecciones.clear();
        if(this.unaSala.getSecciones().size() > 0){
            for (Seccion miSeccion : this.unaSala.ObtenerSecciones()) {                
                this.modelSecciones.addElement(miSeccion);
            }            
        }else{
            this.modelSecciones.addElement("No se encontraron secciones");
        }
    }
    
    public void cargarFilas(){
        this.modelFilas.clear();        
        if(this.unaSeccion.obtenerFilas().size() > 0){
            for (Fila unaFila : this.unaSeccion.obtenerFilas()) {
                this.modelFilas.addElement(unaFila);
            }
        }else{
            this.modelFilas.addElement("No se encontraron filas");
        }
    }
    public void seleccionearSala(){
        if(this.modelSalas.size() > 0){
            this.unaSala = (Sala) this.modelSalas.getElementAt(this.lstSalas.getSelectedIndex());
        }
    }
    public void seleccionarSeccion(){
        if(this.modelSecciones.size() > 0){
            this.unaSeccion = (Seccion) this.modelSecciones.getElementAt(this.lstSecciones.getSelectedIndex());
        }
        
    }
    public void seleccionarFila(){
        if(this.modelFilas.size() > 0){
            this.unaFila = (Fila) this.modelFilas.getElementAt(this.lstFilas.getSelectedIndex());
            this.txtNro.setText(this.unaFila.getNro());
            if(this.unaFila.isEstado()){
                this.rdActiva.setSelected(true);
                this.rdInactiva.setSelected(false);
            }else{
                this.rdActiva.setSelected(false);
                this.rdInactiva.setSelected(true);
            }
        }
    }
    public void guardar() {
        try{
        if(this.unaFila == null){
            this.unaSeccion.agregarFila(this.txtNro.getText());
        }else{
            boolean estado = this.rdActiva.isSelected();
            this.unaFila.modificar(this.txtNro.getText(),estado);
        }
        }
        catch(Exception ex){
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
        this.cargarFilas();
        this.txtNro.setText("");
        this.txtNro.setEnabled(false);
        this.rdActiva.setSelected(false);
        this.rdInactiva.setSelected(false);
        this.rdActiva.setEnabled(false);
        this.rdInactiva.setEnabled(false);
        this.btnGuardar.setEnabled(false);
        this.btnNuevo.setEnabled(true);
    }
    public void nuevo(){
        this.unaFila = null;
        this.txtNro.setText("");
        this.txtNro.setEnabled(true);
        this.btnNuevo.setEnabled(false);
        this.rdActiva.setSelected(true);
        this.rdActiva.setEnabled(true);
        this.rdInactiva.setSelected(false);
        this.rdInactiva.setEnabled(true);  
        this.btnGuardar.setEnabled(true);
    }
        

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstSalas = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstSecciones = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstFilas = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        txtNro = new javax.swing.JTextField();
        rdActiva = new javax.swing.JRadioButton();
        rdInactiva = new javax.swing.JRadioButton();
        btnNuevo = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Salas");

        lstSalas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstSalas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstSalasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(lstSalas);

        jLabel2.setText("Secciones");

        lstSecciones.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstSecciones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstSeccionesMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(lstSecciones);

        jLabel3.setText("Filas");

        lstFilas.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstFilas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstFilasMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(lstFilas);

        jLabel4.setText("Nro");

        rdActiva.setText("Activa");

        rdInactiva.setText("Deshabilitada");

        btnNuevo.setText("Nuevo");
        btnNuevo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnNuevoMouseClicked(evt);
            }
        });
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnAceptar.setText("Aceptar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(109, 109, 109))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(txtNro, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(rdActiva)
                            .addComponent(rdInactiva)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnNuevo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnAceptar)
                                    .addComponent(btnGuardar)))))
                    .addComponent(jLabel3))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtNro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rdActiva)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rdInactiva)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnNuevo)
                            .addComponent(btnGuardar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAceptar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane3))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lstSalasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstSalasMouseClicked
        // TODO add your handling code here:
        this.seleccionearSala();
        this.cargarSecciones();
    }//GEN-LAST:event_lstSalasMouseClicked

    private void lstSeccionesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstSeccionesMouseClicked
        // TODO add your handling code here:
        this.seleccionarSeccion();
        this.cargarFilas();
    }//GEN-LAST:event_lstSeccionesMouseClicked

    private void lstFilasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstFilasMouseClicked
        // TODO add your handling code here:
        this.seleccionarFila();
    }//GEN-LAST:event_lstFilasMouseClicked

    private void btnNuevoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnNuevoMouseClicked
        // TODO add your handling code here:
        this.nuevo();
    }//GEN-LAST:event_btnNuevoMouseClicked

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        this.guardar();
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        // TODO add your handling code here:
        this.nuevo();
    }//GEN-LAST:event_btnNuevoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main2(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmFila.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmFila.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmFila.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmFila.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmFila dialog = new FrmFila(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList lstFilas;
    private javax.swing.JList lstSalas;
    private javax.swing.JList lstSecciones;
    private javax.swing.JRadioButton rdActiva;
    private javax.swing.JRadioButton rdInactiva;
    private javax.swing.JTextField txtNro;
    // End of variables declaration//GEN-END:variables
}
